<?php 
    if(!isset($_GET['ajax']) || ((bool)$_GET['ajax']) === false){
        get_header(); 
    }
?>
<!-- layout -->
<div class="rootFrame main">
    <div class="main-logo">
        &nbsp;
    </div>
    <div class="main-forecast">
        <div class="wrapper">
            <div class="column forecastToday">
                <div class="title">Dnes</div>
                <div style="padding: 15px 0 0;text-align: center">
                    <img src="<?php print get_bloginfo('stylesheet_directory'); ?>/images/icons/weather/na.png" class="icon" />                
                </div>
                <div class="temperature">N/A °C</div>
            </div>
            <div class="column forecastTommorow">
                <div class="title">Zítra</div>
                <div style="padding: 15px 0 0;text-align: center">
                    <img src="<?php print get_bloginfo('stylesheet_directory'); ?>/images/icons/weather/na.png" class="icon" />                
                </div>
                <div class="temperature">N/A °C</div>
            </div>
        </div>
        <div class="weatherSource">
            <span style="color:#cf7bca">Zdroj :</span> World Weather Online
        </div>
    </div>
    <div id="test" class="preloading main-mainframe master">  
        <!-- place for main loop -->
    </div>
    <div class="main-info">
        <div class="clocks" id="clocks">
          <!-- 10:21 -->  
        </div>
        <div class="date" id="date">
          <!-- 1.1.2011 -->   
        </div>
        <div class="nameday">
          Svátek má <span id="nameday"><!-- Michal --></span>
        </div>
    </div>
    <div class="main-rssfeed">
        <div class="wrapper" id="feed">
            Berdych se Štěpánkem zařídili třetí bod, tenisté zůstávají mezi elitou ...
        </div>
    </div>
    <div class="main-pager">
        <div class="wrapper">
            1/3
        </div>
    </div>
    <div class="main-exchange">
        <div class="wrapper exchange">
            <div class="eur">EUR <span style="color:#cc88c1">:</span> <span class="rate">0,000</span> Kč <img src="<?php print get_bloginfo('stylesheet_directory'); ?>/images/icons/currency/down.png" class="rateIcon" /></div> 
            <div class="usd">USD <span style="color:#cc88c1">:</span> <span class="rate">0,000</span> Kč <img src="<?php print get_bloginfo('stylesheet_directory'); ?>/images/icons/currency/down.png" class="rateIcon" /></div>
        </div>
    </div>
</div>
<!-- logic -->
<script type="text/javascript"> 
    //create namespace for this slide using anonymous function
    (function(){
        var weatherTimer = todayInfoTimer = exchangeRateTimer = feedScroll = null;
        //retrive periodicaly weather forecast 
        var weatherForecast = function(){
            ads_pipeline.toJSON({
                service   : 'worldweatheronline.com:simple',
                onSuccess : function(responseJSON, responseText){
                    $$('.forecastToday .icon').each(function(el){
                        el.set('src', templateDir + "/images/icons/weather/" + responseJSON.data.weather[0].weatherCode + ".png");
                    });
                    $$('.forecastToday .temperature').each(function(el){
                        el.set('html', responseJSON.data.weather[0].tempMaxC+' °C');
                    });
                    $$('.forecastTommorow .icon').each(function(el){
                         el.set('src', templateDir + "/images/icons/weather/" + responseJSON.data.weather[1].weatherCode + ".png");
                    });
                    $$('.forecastTommorow .temperature').each(function(el){
                        el.set('html', responseJSON.data.weather[1].tempMaxC+' °C');
                    });
                }
            });
        }
        //retrive periodicaly RSS feed
        var rssFeed = function(el){
            ads_pipeline.toJSON({
                service   : 'idnes.cz',
                onSuccess : function(responseJSON, responseText){
                    console.log('rss feed');
                    $('feed').empty();
                    feedScroll = new Fx.Marquee(el, {
                        duration: 500
                    });
                    var announce = null;
                    Object.each(responseJSON.channel.item, function(item,key){
                        if(announce != null){
                            announce = announce.chain(function(){
                                feedScroll.announce({
                                    message: arguments[0],
                                    delay: 5000,
                                    revert: false
                                });
                            }.pass(item.title + '.'));
                        }else{
                            announce = feedScroll.announce({
                                message: item.title + '.',
                                delay: 5000,
                                revert: false
                            });
                        }
                    });
                    announce.chain(function(){
                       rssFeed(el); 
                    });
                }
            });
        }
        //retrive periodicaly exchange rates
        var exchangeRate = function(){
            try{        
                ads_pipeline.toJSON({
                    service   : 'cnb.cz:today',
                    onSuccess : function(todayJSON, todayText){
                        ads_pipeline.toJSON({
                            service   : 'cnb.cz:yesterday',
                            onSuccess : function(yesterdayJSON, yesterdayText){
                                //alert('ok');
                                //console.log(yesterdayJSON.usd.rate);
                                $$('.exchange .eur .rate').each(function(el){
                                    el.set('html', todayJSON.eur.rate);
                                });
                                $$('.exchange .usd .rate').each(function(el){
                                    el.set('html', todayJSON.usd.rate);
                                });
                                $$('.exchange .eur .rateIcon').each(function(el){
                                    el.set('src', templateDir + "/images/icons/currency/" + (todayJSON.eur.rate < yesterdayJSON.eur.rate?'down':'up') + ".png");
                                });
                                $$('.exchange .usd .rateIcon').each(function(el){
                                    el.set('src', templateDir + "/images/icons/currency/" + (todayJSON.usd.rate < yesterdayJSON.usd.rate?'down':'up') + ".png");
                                });
                            }
                        });
                    }
                });
            }catch(e){
                clearInterval(exchangeRateTimer);
            }
        } 

        //clocks
        var clocks = new Clocks({element: 'clocks'});

        //nameday
        var todayinfo = function(){ 
            try{
                clocks.showTime();
                $('nameday').set('html', getTodayNameday());
                $('date').set('html', new Date().format('%d.%m.%Y'));
            }catch(e){
                clearInterval(todayInfoTimer);
            }
        };

        //register widget
        var slide = ads_client.getPresentation().getCurrentSlide();    
        slide.registerWidget('widgets', { start : function(){  
            //weather forecast
            weatherForecast();
            weatherTimer = weatherForecast.periodical(60*60*1000);  
            //feed
            rssFeed.delay(4000,this,[$('feed')]);
            //exchange rates
            exchangeRate();
            exchangeRateTimer = exchangeRate.periodical(10000);
            //today info        
            todayinfo();
            todayInfoTimer = todayinfo.periodical(1000);
        }, stop : function(){
            clearInterval(weatherTimer);
            clearInterval(exchangeRateTimer);
            clearInterval(todayInfoTimer);    
            feedScroll.clearChain();
        }});
    })();

</script>
<?php 
    if(!isset($_GET['ajax']) || ((bool)$_GET['ajax']) === false){
        get_footer(); 
    }
?>